<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/6/4
 * Time: 20:43
 */

namespace app\model;


class Users extends Model
{
    public function __construct($db)
    {
        parent::__construct($db,"users");
    }

    /**
     * 获取用户信息
     * @param $userUUID
     * @return array|mixed
     */
    public function getUserInfo($userUUID)
    {
        $userInfo = $this->getInfo(["user_uuid"=>$userUUID],"head_image,nickname");
        if($userInfo){
            $userMaterialModel = new UserMaterial();
            $userMaterialModel->setDb($this->db);
            $userInfo["video_nums"] = $userMaterialModel->getUserMaterialNums($userUUID);
        }
        return $userInfo;
    }

    /**
     *获取用户关注量
     */
    public function follow($uuid){
        $this->db->select("id,my_uuid,other_uuid");
        $this->db->from("material_follow");
        $this->db->where(["my_uuid"=>$uuid]);
        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     *获取用户被关注量
     */
    public function is_follow($uuid){
        $this->db->select("id,my_uuid,other_uuid");
        $this->db->from("material_follow");
        $this->db->where(["other_uuid"=>$uuid]);
        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     *获取其它用户关注量
     */
    public function other_follow($uuid){
        $this->db->select("id,my_uuid,other_uuid");
        $this->db->from("material_follow");
        $this->db->where(["my_uuid"=>$uuid]);
        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     *他人主页获取用户关注量
     */
    public function follows($uuid){
        $this->db->select("id,my_uuid,other_uuid");
        $this->db->from("material_follow");
        $this->db->where(["my_uuid"=>$uuid]);
        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }
    /**
     *他人主页获取用户被关注量
     */
    public function is_follows($uuid){
        $this->db->select("id,my_uuid,other_uuid");
        $this->db->from("material_follow");
        $this->db->where(["other_uuid"=>$uuid]);
        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }


    /**
     *获取用户自己信息
     */
    public function my_home($uuid,$type=1){
        if($type==1){
            $this->db->select("uuid,nick_name,nick_pic,user_autograph");
        }else if($type==2){
            $this->db->select("uuid,nick_name,nick_pic,test_user_autograph");
        }

        $this->db->from("users");
        $this->db->where(["uuid"=>$uuid]);
        $query =  $this->db->get();
        $datas = $query->one();
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     * 获取我的和他人用户作品
     */
    public function my_material($page,$pageSize,$uuid,$type=1){
        $this->db->select("id,uuid,video_uuid,pic,video_url,examine,width2,height2,first_pic3,share_pic,count_material_love,count_material_share");
        $this->db->from("user_material");
        if($type==1){
            $this->db->where(["uuid"=>$uuid,"mix_status"=>1,"del1"=>2]);
            $this->db->order_by(["examine"=>"desc","add_time"=>"desc"]);
        }else if($type==2){
            $this->db->where(["uuid"=>$uuid,"examine"=>3,"mix_status"=>1,"del1"=>2]);
            $this->db->order_by(["id"=>"desc"]);
            $this->db->order_by(["add_time"=>"asc"]);
        }
        $this->db->limit($pageSize,($page-1)*$pageSize);
        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     * 获取我的和他人用户作品
     */
    public function other_material($page,$pageSize,$uuid){
        $this->db->select("id,uuid,video_uuid,pic,video_url,examine,width2,height2,first_pic3,share_pic,count_material_love,count_material_share");
        $this->db->from("user_material");
        $this->db->where(["uuid"=>$uuid,"examine"=>3,"mix_status"=>1,"del1"=>2]);
        $data['total'] =  $this->db->count_all_results();
        $this->db->order_by(["id"=>"desc"]);
        $this->db->limit($pageSize,($page-1)*$pageSize);
        $query =  $this->db->get();
        $data['row'] = $query->all();
//        echo $this->db->last_query();exit;
        return $data;
    }

    /**
     * 查询用户在点赞表的记录
     */
    public function my_zan($page,$pageSize,$uuid){
        $this->db->select("video_uuid,other_id");
        $this->db->from("material_zan");
        $this->db->where(["my_uuid"=>$uuid]);
        $this->db->order_by(["add_time"=>"desc"]);
        $this->db->limit($pageSize,($page-1)*$pageSize);
        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     * 用户喜欢的视频
     */
    public function my_love($id,$other_id){
        $this->db->select("id,video_desc,video_uuid,video_url,pic,nick_name,nick_pic,width2,height2,first_pic3,share_pic,count_material_love,count_material_share");
        $this->db->from("user_material");
        $this->db->where(["del1"=>2,"examine"=>3,'video_uuid'=>$id,'id'=>$other_id]);
//        $this->db->where(["examine"=>3,'video_uuid'=>$id]);
        $total =  $this->db->count_all_results();
        $query =  $this->db->get();
        $datas = $query->one();
//        $data['total']=$total;
//        $data['row']=$datas;
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     * 用户的关注列表
     */
    public function my_follow_list($page,$pageSize,$uuid,$type=1){
        $this->db->select("a.id,a.my_uuid,a.other_uuid,b.nick_name,b.nick_pic,b.test_user_autograph");
        $this->db->from("material_follow as a");
        $this->db->join("users as b","a.other_uuid=b.uuid");
        $this->db->where(["my_uuid"=>$uuid]);
//        $this->db->order_by(["add_time"=>"desc"]);
        if($type==1){
            $this->db->limit($pageSize,($page-1)*$pageSize);
        }

        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     * 用户的关注列表
     */
    public function my_is_follow_list($page,$pageSize,$uuid){
            $this->db->select("a.id,a.my_uuid,a.other_uuid,b.nick_name,b.nick_pic,b.test_user_autograph");
            $this->db->from("material_follow as a");
            $this->db->join("users as b","a.my_uuid=b.uuid");
            $this->db->where(["other_uuid"=>$uuid]);
//        $this->db->order_by(["add_time"=>"desc"]);
            $this->db->limit($pageSize,($page-1)*$pageSize);


        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     *编辑用户个性签名
     */
    public function update_autograph($uuid,$autograph){
        return $this->db->update("users",['user_autograph'=>$autograph,'is_status'=>2],['uuid'=>$uuid]);
    }

    /**
     * 查询我的作品个数
     */
    public function count_material($uuid,$type=1){
        $this->db->select("id");
        $this->db->from("user_material");
        if($type==1){
            $this->db->where(["uuid"=>$uuid,"mix_status"=>1,"del1"=>2]);
        }else if($type==2){
            $this->db->where(["uuid"=>$uuid,"del1"=>2,"examine"=>3,"mix_status"=>1]);
        }
        $query =  $this->db->get();
        $datas = $query->all();
        return $datas;
    }

    /**
     * 查询我的作品个数
     */
    public function count_love($uuid){
        $this->db->select("video_uuid,other_id");
        $this->db->from("material_zan");
        $this->db->where(["my_uuid"=>$uuid]);
        $query =  $this->db->get();
        $datas = $query->all();
        $da1=[];
        if(!empty($datas)){
            foreach($datas as $k=>$v){
                $this->db->select("id");
                $this->db->from("user_material");
                $this->db->where(["del1"=>2,"examine"=>3,'video_uuid'=>$v['video_uuid'],'id'=>$v['other_id']]);
//                $this->db->where(["examine"=>3,'video_uuid'=>$v['video_uuid']]);
                $query =  $this->db->get();
                $data1 = $query->one();
                $da1[]=$data1;
            }
            return $da1;
        }else{
            return $da1;
        }
    }

    /**
     * 获取用户所有获赞数
     */
    public function count_zan($uuid){
        $this->db->select("video_uuid,other_id");
        $this->db->from("material_zan");
        $this->db->where(["other_uuid"=>$uuid]);
        $datas['total']=$this->db->count_all_results("",false);
        $query =  $this->db->get();
//        echo $this->db->last_query();exit;
        $datas['row'] = $query->all();
        return $datas;
    }

    /**
     * 获取用户所有获赞数
     */
    public function count_zan123($uuid){
        $this->db->select("id,count_material_love");
        $this->db->from("user_material");
        $this->db->where(["uuid"=>$uuid,"examine"=>3,"mix_status"=>1,"del1"=>2]);
        $query =  $this->db->get();
//        echo $this->db->last_query();exit;
        $datas= $query->all();
        return $datas;
    }

    /**
     *用户删除自己作品
     */
    public function update_material($id,$uuid){
        $res=$this->db->update("user_material",['del1'=>1],['id'=>$id]);
//        $res=$this->db->update("user_material",['del1'=>1],['id'=>$id,'uuid'=>$uuid]);
        return $res;
    }

    /**
     * 删除自己的作品被别人点赞的
     */
    public function del_zan($id,$video_uuid,$uuid){
        //先去查询是否有这个点赞
        $this->db->select("id");
        $this->db->from("material_zan");
        $this->db->where(['other_id'=>$id,"video_uuid"=>$video_uuid,"other_uuid"=>$uuid]);
        $query =  $this->db->get();
//        echo $this->db->last_query();exit;
        $row = $query->one();
        if(empty($row)){
            return true;
        }else{
            return $this->db->delete("material_zan",['other_id'=>$id,"video_uuid"=>$video_uuid,"other_uuid"=>$uuid]);
        }
    }


}